/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.institution.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.institution.service.api.ConformanceService;
import com.koron.institution.uitls.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "Conformance", description = "合规评价Api类")
@RestController
@RequestMapping(value = "/conformance")
public class ConformanceController {

    @Autowired
    private ConformanceService conformanceService;

    @ApiOperation("获取合规评价列表")
    @RequestMapping(value = "/getConformanceList.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceYear", value = "合规评价年份", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态：0，草稿，1.审核中，2，审核通过", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "institutionName", value = "法规制度名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer")
    })
    public MessageBean<?> getReviseList(String corpCode, Integer conformanceYear, String institutionName, Integer page, Integer pageSize, Integer state,
                                        @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return conformanceService.getConformanceList(corpCode, conformanceYear, institutionName, page, pageSize, state, user);
    }

    @ApiOperation("获取合规评价草稿列表")
    @RequestMapping(value = "/getConformanceDraftList.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceYear", value = "修订年份", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态：0，草稿，1.审核中，2，审核通过", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "institutionName", value = "法规制度名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer")
    })
    public MessageBean<?> getReviseDraftList(String corpCode, Integer conformanceYear, String institutionName, Integer page, Integer pageSize, Integer state,
                                             @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return conformanceService.getConformanceDraftList(corpCode, conformanceYear, institutionName, page, pageSize, state, user);
    }


    @ApiOperation("保存合规评价")
    @RequestMapping(value = "/saveOrCommit.htm", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceYear", value = "评审年份", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "operationType", value = "操作类型：0.草稿，1.提交", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approverCodeListStr", value = "审批人code集合字符串", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceAssessmentListStr", value = "合规评价集合字符串", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "jobsTaskId", value = "任务id", paramType = "query", dataType = "String"),
    })
    @Log(title = "保存合规评价",businessModule = "合规评价", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> saveOrCommit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode,
                                       Integer operationType, String conformanceAssessmentListStr, String approverCodeListStr, String jobsTaskId, Integer conformanceYear) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return conformanceService.saveOrCommit(user, corpCode, operationType, conformanceAssessmentListStr, approverCodeListStr, jobsTaskId, conformanceYear);
    }


    @ApiOperation("批量编辑合规评价")
    @RequestMapping(value = "/batchEdit.htm", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approverCodeListStr", value = "审批人code集合字符串", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceAssessmentHistoryListStr", value = "合规评价集合字符串", required = true, paramType = "query", dataType = "String"),
    })
    @Log(title = "批量编辑合规评价",businessModule = "合规评价", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> batchEdit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode,
                                    String conformanceAssessmentHistoryListStr, String approverCodeListStr) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return conformanceService.batchEdit(user, corpCode, conformanceAssessmentHistoryListStr, approverCodeListStr);
    }


    @ApiOperation("批量废除合规评价")
    @RequestMapping(value = "/batchDiscard.htm", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approverCodeListStr", value = "审批人code集合字符串", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceAssessmentHistoryListStr", value = "合规评价集合字符串", required = true, paramType = "query", dataType = "String"),
    })
    @Log(title = "批量废除合规评价",businessModule = "合规评价", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> batchDiscard(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode,
                                       String conformanceAssessmentHistoryListStr, String approverCodeListStr) {

        return conformanceService.batchDiscard(user, corpCode, conformanceAssessmentHistoryListStr, approverCodeListStr);
    }

    @ApiOperation("合规评价导出")
    @RequestMapping(value = "/exports.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "文件标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fileCode", value = "文件编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "recordCode", value = "记录编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "version", value = "版/次", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceYear", value = "评审年份", required = true, paramType = "query", dataType = "String")
    })
    public HttpEntity<?> exports(String title, String fileCode, String recordCode, String version, Integer conformanceYear, String corpCode, String corpName, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        Map<String, Object> map = new HashMap<>();
        if (conformanceYear == null || StringUtils.isBlank(corpCode) || StringUtils.isBlank(title)
                || StringUtils.isBlank(fileCode) || StringUtils.isBlank(recordCode) || StringUtils.isBlank(version)) {
            return null;
        }
        map = conformanceService.queryExportData(title, fileCode, recordCode, version, user, corpCode, corpName, conformanceYear);
        if (map == null) {
            return null;
        }
        File targetFile = new File("static/excel/Conformance.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/Conformance.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
        return Excel.createFile(title != null ? title : "合规评价列表", targetFile, "Conformance.btl", map);
    }


    @ApiOperation("流程中的业务数据")
    @ApiImplicitParam(name = "ids", value = "流程关联的业务ids", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getProcessData.htm", method = {RequestMethod.GET})
    public MessageBean<?> getProcessData(String ids) {
        return conformanceService.getProcessData(ids);
    }


    @ApiOperation("根据名称获取法规下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "法规名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "conformanceYear", value = "修订年份", paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/getLawSelect.htm", method = {RequestMethod.GET})
    public MessageBean<?> getInstitutionSelect(String corpCode, String name, Integer conformanceYear) {
        return conformanceService.getLawSelect(corpCode, name, conformanceYear);
    }


    @ApiOperation("删除草稿")
    @RequestMapping(value = "/deleteDraft.htm", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "conformanceAssessmentId", value = "评估修订id", required = true, paramType = "query", dataType = "String"),
    })
    @Log(title = "删除草稿",businessModule = "合规评价", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> deleteDraft(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                      String conformanceAssessmentId) {

        return conformanceService.deleteDraft(conformanceAssessmentId);
    }


    @ApiOperation("通过id集合获取合规评估列表")
    @RequestMapping(value = "/getConformanceHistoryListByIds.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessIds", value = "业务id集合字符串", required = true, paramType = "query", dataType = "String")
    })
    public MessageBean<?> getConformanceHistoryListByIds(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String businessIds) {
        return conformanceService.getConformanceHistoryListByIds(businessIds);
    }
}
